home *** CD-ROM | disk | FTP | other *** search
- /* ------------------------------------------------------------ */
- /*
- HTTrack Website Copier, Offline Browser for Windows and Unix
- Copyright (C) Xavier Roche and other contributors
-
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-
- Important notes:
-
- - We hereby ask people using this source NOT to use it in purpose of grabbing
- emails addresses, or collecting any other private information on persons.
- This would disgrace our work, and spoil the many hours we spent on it.
-
-
- Please visit our Website: http://www.httrack.com
- */
-
-
- /* ------------------------------------------------------------ */
- /* File: httrack.c subroutines: */
- /* various tools (filename analyzing ..) */
- /* Author: Xavier Roche */
- /* ------------------------------------------------------------ */
-
- #include "htstools.h"
-
- /* specific definitions */
- #include "htsbase.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /* END specific definitions */
-
-
- // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
- // [adr et fil sont des buffers de 1ko]
- // 0 : ok
- // -1 : erreur
- // -2 : protocole non supportΘ (ftp)
- int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
- int ok=0;
-
- adr[0]='\0'; fil[0]='\0'; //effacer buffers
-
- // lien non vide!
- if (strnotempty(lien)==0) return -1; // erreur!
-
- // filtrer les parazites (mailto & cie)
- if (strfield(lien,"mailto:")) { // ne pas traiter
- ok=-1;
- }
- else if (strfield(lien,"news:")) { // ne pas traiter
- ok=-1;
- }
- //else if (strfield(lien,"file:")) { // ne pas traiter
- // ok=-1;
- //}
- else if (strfield(lien,"javascript:")) { // ne pas traiter
- ok=-1;
- }
- else if (strstr(lien,":/")) { // c'est une URL?
- if (strfield(lien,"http:")) {
- if (ident_url(lien,adr,fil)==-1) {
- ok=-1; // erreur URL
- }
- } else if (strfield(lien,"file:")) {
- if (ident_url(lien,adr,fil)==-1) {
- ok=-1; // erreur URL
- }
- } else if (strfield(lien,"ftp:")) {
- if (ftp_available()) { // ftp supportΘ
- if (ident_url(lien,adr,fil)==-1) {
- ok=-1; // erreur URL
- }
- } else {
- ok=-2; // non supportΘ
- }
- } else { // je ne connais pas ce protocole!!
- ok=-1;
- }
- } else { // c'est un lien relatif
- char* a;
-
- // On forme l'URL complΦte α partie de l'url actuelle
- // et du chemin actuel si besoin est.
-
- // copier adresse
- if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
- strcpy(adr,origin_adr); // mΩme adresse
-
- /* bogus form: http:relative.html */
- if (strfield(lien,"http:"))
- lien+=5;
-
- if (*lien!='/') { // sinon c'est un lien absolu
- a=strchr(origin_fil,'?');
- if (!a) a=origin_fil+strlen(origin_fil);
- while((*a!='/') && ( ((int) a) > ((int) origin_fil)) ) a--;
- if (*a=='/') { // ok on a un '/'
- if ( (((int) a)-((int) origin_fil)+1+strlen(lien)) < HTS_URLMAXSIZE) {
- // copier chemin
- strncpy(fil,origin_fil,((int) a)-((int) origin_fil)+1);
- *(fil + ((int) a)-((int) origin_fil)+1)='\0';
-
- // copier chemin relatif
- if (((int) strlen(fil)+(int) strlen(lien))<HTS_URLMAXSIZE) {
- strcat(fil,lien + ((*lien=='/')?1:0) );
- // simplifier url pour les ../
- fil_simplifie(fil);
- } else
- ok=-1; // erreur
- } else { // erreur
- ok=-1; // erreur URL
- }
- } else { // erreur
- ok=-1; // erreur URL
- }
- } else { // chemin absolu
- // copier chemin directement
- strcat(fil,lien);
- } // *lien!='/'
- } else
- ok=-1;
-
- } // test news: etc.
-
- // case insensitive pour adresse
- {
- char *a=jump_identification(adr);
- while(*a) {
- if ((*a>='A') && (*a<='Z'))
- *a+='a'-'A';
- a++;
- }
- }
-
- return ok;
- }
-
-
-
-
-
- // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
- // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
- int lienrelatif(char* s,char* link,char* curr_fil) {
- char _curr[HTS_URLMAXSIZE*2];
- char newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
- char* curr;
- //int n=0;
- char* a;
- int slash=0;
- //
- newcurr_fil[0]='\0'; newlink[0]='\0';
- //
-
- // patch: Θliminer les ? (paramΦtres) sinon bug
- if ( (a=strchr(curr_fil,'?')) ) {
- strncat(newcurr_fil,curr_fil,(int) a-(int) curr_fil);
- curr_fil = newcurr_fil;
- }
- if ( (a=strchr(link,'?')) ) {
- strncat(newlink,link,(int) a-(int) link);
- link = newlink;
- }
-
- // recopier uniquement le chemin courant
- curr=_curr;
- strcpy(curr,curr_fil);
- if ((a=strchr(curr,'?'))==NULL) // couper au ? (params)
- a=curr+strlen(curr)-1; // pas de params: aller α la fin
- while((*a!='/') && ((int) a>(int) curr)) a--; // chercher dernier / du chemin courant
- if (*a=='/') *(a+1)='\0'; // couper dernier /
-
- // "effacer" s
- s[0]='\0';
-
- // sauter ce qui est commun aux 2 chemins
- {
- char *l,*c;
- if (*link=='/') link++; // sauter slash
- if (*curr=='/') curr++;
- l=link;
- c=curr;
- // couper ce qui est commun
- #if HTS_CASSE
- while ((*link==*curr) && (*link!=0)) {link++; curr++; }
- #else
- while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
- #endif
- // mais on veut un rΘpertoirer entier!
- // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
- while(((*link!='/') || (*curr!='/')) && ((int) link>(int) l)) { link--; curr--; }
- //if (*link=='/') link++;
- //if (*curr=='/') curr++;
- }
-
- // calculer la profondeur du rΘpertoire courant et remonter
- // LES ../ ONT ETE SIMPLIFIES
- a=curr;
- if (*a=='/') a++;
- while(*a) if (*(a++)=='/') strcat(s,"../");
- //if (strlen(s)==0) strcat(s,"/");
-
- if (slash) strcat(s,"/"); // garder absolu!!
-
- // on est dans le rΘpertoire de dΘpart, copier
- strcat(s,link + ((*link=='/')?1:0) );
-
- // on a maintenant une chaine de la forme ../../test/truc.html
- return 0;
- }
-
-
- // conversion chemin de fichier/dossier vers 8-3
- void long_to_83(char* n83,char* save) {
- n83[0]='\0';
-
- while(*save) {
- char fn83[16],fnl[256];
- int i=0;
- while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
- fnl[i]='\0';
- // conversion
- longfile_to_83(fn83,fnl);
- strcat(n83,fn83);
-
- save+=i;
- if (*save=='/') { strcat(n83,"/"); save++; }
- }
- }
-
-
- // conversion nom de fichier/dossier vers 8-3
- void longfile_to_83(char* n83,char* save) {
- int i=0,j=0;
- char nom[8+1]="";
- char ext[3+1]="";
-
- while((i<8) && (save[j]) && (save[j]!='.')) { if (save[j]!=' ') { nom[i]=save[j]; i++; } j++; } // recopier nom
- nom[i]='\0';
- if (save[j]) { // il reste au moins un point
- i=strlen(save)-1;
- while((i>0) && (save[i]!='.') && (save[i]!='/')) i--; // rechercher dernier .
- if (save[i]=='.') { // point!
- int j=0;
- i++;
- while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
- ext[j]='\0';
- }
- }
- // corriger vers 8-3
- n83[0]='\0';
- strncat(n83,nom,8);
- if (strnotempty(ext)) {
- strcat(n83,".");
- strncat(n83,ext,3);
- }
- }
-
- // Θcrire backblue.gif
- int verif_backblue(char* base) {
- int ret=0;
- if (fsize(fconcat(base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN) {
- FILE* fp = filecreate(fconcat(base,"backblue.gif"));
- if (fp) {
- if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
- ret=1;
- fclose(fp);
- usercommand(0,NULL,fconcat(base,"backblue.gif"));
- } else
- ret=1;
- //
- fp = filecreate(fconcat(base,"fade.gif"));
- if (fp) {
- if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
- ret=1;
- fclose(fp);
- usercommand(0,NULL,fconcat(base,"fade.gif"));
- } else
- ret=1;
- }
- return ret;
- }
-
-
- // recherche chaεne de type truc<espaces>=
- // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
- /* SECTION OPTIMISEE:
- #define rech_tageq(adr,s) ( \
- ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
- ( (streql(*adr,*s)) ? \
- (__rech_tageq(adr,s)) \
- : 0 \
- ) \
- : 0\
- )
- */
- /*
- HTS_INLINE int rech_tageq(const char* adr,const char* s) {
- if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) { // <tag < tag etc
- if (streql(*adr,*s)) { // tester premier octet (optimisation)
- return __rech_tageq(adr,s);
- }
- }
- return 0;
- }
- */
- // DeuxiΦme partie
- HTS_INLINE int __rech_tageq(const char* adr,const char* s) {
- int p;
- p=strfield(adr,s);
- if (p) {
- while(is_space(adr[p])) p++;
- if (adr[p]=='=') {
- return p+1;
- }
- }
- return 0;
- }
-
- // tag sans =
- HTS_INLINE int rech_sampletag(const char* adr,const char* s) {
- register int p;
- if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) { // <tag < tag etc
- p=strfield(adr,s);
- if (p) {
- if (!isalnum((unsigned char)adr[p])) { // <srcbis n'est pas <src
- return 1;
- }
- return 0;
- }
- }
- return 0;
- }
-
- // teste si le tag contenu dans from est Θgal α "tag"
- HTS_INLINE int check_tag(char* from,const char* tag) {
- char* a=from+1;
- int i=0;
- char s[256];
- while(is_space(*a)) a++;
- while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
- s[i++]='\0';
- return (strfield2(s,tag)); // comparer
- }
-
- // teste si un fichier dΘpasse le quota
- int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
- int ok=1;
- if (size>0) {
- if (is_hypertext_mime(type)) {
- if (maxhtml>0) {
- if (size>maxhtml)
- ok=0;
- }
- } else {
- if (maxnhtml>0) {
- if (size>maxnhtml)
- ok=0;
- }
- }
- }
- return (!ok);
- }
-
-